<?php
namespace Foam\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\EventManager\EventManagerInterface;
use Zend\View\Model\ViewModel;
use Zend\View\Model\JsonModel;

class CarteController extends AbstractActionController
{
    protected $carteTable;
    protected $produitTable;
    protected $carteproduitTable;
    
    public function setEventManager(EventManagerInterface $events)
    {
        parent::setEventManager($events);
 
        $controller = $this;
        $events->attach('dispatch', function ($e) use ($controller) {
            $controller->layout()->setTemplate('layout/foam');
        }, 100);
 
        return $this;
    }
    
    public function getCarteTable()
    {
        if (!$this->carteTable) {
            $sm = $this->getServiceLocator();
            $this->carteTable = $sm->get('Foam\Model\CarteTable');
        }
        return $this->carteTable;
    }
    
    public function getProduitTable()
    {
        if (!$this->produitTable) {
            $sm = $this->getServiceLocator();
            $this->produitTable = $sm->get('Foam\Model\ProduitTable');
        }
        return $this->produitTable;
    }
    
    public function getCarteProduitTable()
    {
        if (!$this->carteproduitTable) {
            $sm = $this->getServiceLocator();
            $this->carteproduitTable = $sm->get('Foam\Model\CarteProduitTable');
        }
        return $this->carteproduitTable;
    }
    
    public function indexAction()
    {
        return new ViewModel(array(
            'cartes' => $this->getCarteTable()->fetchAll(),
        ));
    }
    
    public function savenewAction()
    {
        $request = $this->getRequest();
        if ($request->isPost()) {
            $id_carte = '';
            $libelle = ($request->getPost()->libelle)?$request->getPost()->libelle:'';
            $description = ($request->getPost()->description)?$request->getPost()->description:'';
            $image = ($request->getPost()->image)?$request->getPost()->image:'';
            $date_debut = ($request->getPost()->date_debut)?$request->getPost()->date_debut:'';
            $date_fin = ($request->getPost()->date_fin)?$request->getPost()->date_fin:'';
            $active = ($request->getPost()->active)?$request->getPost()->active:1;
        }
        
        $this->getCarteTable()->saveNew($id_carte,$libelle,$description,$image,$date_debut,$date_fin,$active);

        $viewModel = new ViewModel(array(
            'message' => 'Nouvelle carte enregistrée',
        ));

        $viewModel->setTerminal(true);
        
        return $viewModel;
    }
    
    public function updateAction()
    {
        $request = $this->getRequest();
        
        if ($request->isPost()) {
            
            $pk = $request->getPost()->pk;
            $name = $request->getPost()->name;
            $value = $request->getPost()->value;
            
            $this->getCarteTable()->xeditUpdate($pk, $name, $value);
        }
    }
    
    public function deleteAction()
    {
        $id = $this->params('id');
        $this->getCarteTable()->deleteCarte($id);
        return $this->redirect()->toRoute('foam/default', array('controller'=>'carte'));
    }
    
    public function xeditableAction()
    {                
        $data = $this->getCarteTable()->getList();

        $jsonModel = new JsonModel($data);
        
        return $jsonModel;
    }
    
    public function attribproduitAction() {
        $id_carte = $this->params('id');
        $request = $this->getRequest();
        $allproducts = $myallproducts = $myallnewsproducts = array(); 
        
        $myproducts = $this->getCarteProduitTable()->getCarteProduit($id_carte);
        foreach($myproducts as $myproduct) {
            $myallproducts[] = $myproduct['id_produit'];
        }

        if ($request->getPost()->selectable) {
            $rows = $request->getPost()->selectable;
            
            // Ajout et mis en tableau des nouvelles sélections
            foreach($rows as $row) {
                if(!in_array($row, $myallproducts))
                    $this->getCarteProduitTable()->attribCarteProduit("", $row, $id_carte, 1, Date("Y-m-d"));
                $myallnewsproducts[] = $row;
            }
            
            // Sortie des produits non présents dans les nouvelles sélections
            foreach($myallproducts as $oldproduct) {
                if(!in_array($oldproduct, $myallnewsproducts))
                    $this->getCarteProduitTable()->updateCarteProduit($oldproduct, $id_carte, 0, Date("Y-m-d"));    
            }
            
            return $this->redirect()->toRoute('foam/default', array('controller'=>'carte'));
        }
        
        $prds = $this->getProduitTable()->getList();
        foreach ($prds as $prd){
            $allproducts[$prd['flibelle']][$prd['id_produit']] = $prd['code']." : ".$prd['libelle'];
        }
        
        return new ViewModel(array(
            'carte' => $this->getCarteTable()->getCarte($id_carte),
            'allproducts' => $allproducts,
            'myallproducts' => $myallproducts,
        ));
    }
}